package com.stlm2.controller.pay;

import com.google.gson.Gson;
import com.stlm2.dems.entity.OrderState;
import com.stlm2.dems.entity.PayChannel;
import com.stlm2.dems.entity.PurchaseOrder;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.GoodsOrderService;
import com.stlm2.dems.service.PurchaseOrderService;
import com.stlm2.dems.service.UserService;
import com.stlm2.dems.user.entity.Account;
import com.stlm2.dems.user.entity.AccountState;
import com.stlm2.dems.user.service.AccountService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Author：fuchunyu
 * Date：2017/11/7
 * Description：
 */
@Controller
@RequestMapping("/balance")
public class BalancePayController {

    @Resource
    private UserService userService;
    @Resource
    private PurchaseOrderService purchaseOrderService;
    @Resource
    private AccountService accountService;
    @Resource
    private GoodsOrderService goodsOrderService;

    @ResponseBody
    @RequestMapping(value = "/pay.json", produces = "application/json;charset=utf-8")
    public String pay(String companyCode,Integer payChannelValue, String code, String payPassword, String token, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        if (StringUtils.isEmpty(code))
            return CodeContent.returnFailure("请提供订单！");
        PayChannel payChannel = PayChannel.getByIntValue(payChannelValue);
        if (payChannel == null)
            return CodeContent.returnFailure("请传入payChannelValue");
        User user = userService.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();
        PurchaseOrder order = purchaseOrderService.getByCode(code);
        if (order == null || !order.getUserId().equals(user.getId()))
            return CodeContent.returnFailure("订单不存在");
        if (!OrderState.Create.equals(order.getOrderState()) || order.getPayMoney() == null || order.getPayMoney().doubleValue() <= 0)
            return CodeContent.returnFailure("订单" + order.getOrderState().toString() + "，不能支付");

        if (payChannel.equals(PayChannel.Balance)) {        //余额支付
            //检测支付密码
            Account account = accountService.getByUserId(user.getId());
            if (account == null)
                return CodeContent.returnFailure("账户信息为空！");
            if (StringUtils.isBlank(account.getPassword()))
                return CodeContent.returnFailure("未设置支付密码!");
            if (StringUtils.isBlank(payPassword))
                return CodeContent.returnFailure("帐户密码不能为空！");

            if (AccountState.Locked.equals(account.getAccountState()))
                return CodeContent.returnFailure("帐号已锁定！");
            else {

                if (!payPassword.equals(account.getPassword()))
                    return CodeContent.returnFailure("支付密码错误!");
            }
            BigDecimal balance = account.getAccountMoney();

            if (order.getPayMoney().doubleValue() < 0 || order.getPayMoney().compareTo(balance) == 1)
                return CodeContent.returnFailure("余额不足！");
            PurchaseOrder tempOrder=null;
            try {
                tempOrder = goodsOrderService.pay(order, PayChannel.Balance, null,companyCode);
            }catch (Exception e){
                return CodeContent.returnFailure(e.getMessage());
            }

            if (tempOrder != null && OrderState.Payment.equals(tempOrder.getOrderState())) {
                return CodeContent.returnSuccess(null);
            } else {
                return CodeContent.returnFailure("支付失败!");
            }
        } else {
            return CodeContent.returnFailure("支付失败!");
        }
    }
}
